package com.sjm.suchbear.minibookmall.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.BookDB;
import com.sjm.suchbear.minibookmall.EditInfoActivity;
import com.sjm.suchbear.minibookmall.LoginActivity;
import com.sjm.suchbear.minibookmall.OrderListActivity;
import com.sjm.suchbear.minibookmall.R;
import com.sjm.suchbear.minibookmall.Utils.SP;
import com.sjm.suchbear.minibookmall.Utils.T;
import com.sjm.suchbear.minibookmall.action.UserManage;
import com.sjm.suchbear.minibookmall.model.UserModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MyFragment extends Fragment {


    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.tv_waitPay)
    TextView tvWaitPay;
    @BindView(R.id.tv_hadPay)
    TextView tvHadPay;
    @BindView(R.id.tv_orderList)
    TextView tvOrderList;
    @BindView(R.id.tv_myCost)
    TextView tvMyCost;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_changeInfo)
    TextView tvChangeInfo;
    Unbinder unbinder;

    private UserModel userModel;
    private SQLiteDatabase dbread;


    public MyFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, view);
        BookDB db = new BookDB(getActivity());
        dbread = db.getReadableDatabase();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        llHeaderBack.setVisibility(View.GONE);
        tvListTitle.setText("我的");
        userModel = UserManage.findUserByID(dbread, SP.getUid(getActivity()));
        tvName.setText("收货人:      ".concat(userModel.getRealname()));
        tvPhone.setText("手机号:      ".concat(userModel.getPhone()));
        tvAddress.setText("收货地址:   ".concat(userModel.getAddress()));
        tvMyCost.setText("¥".concat(userModel.getCost()));
        ivHeaderFunctionIco.setImageResource(R.drawable.exit);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_waitPay, R.id.tv_hadPay, R.id.tv_orderList, R.id.tv_changeInfo, R.id.ll_header_function})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tv_waitPay:
                intent = new Intent(getActivity(), OrderListActivity.class);
                intent.putExtra("model", 1);
                startActivity(intent);
                break;
            case R.id.tv_hadPay:
                intent = new Intent(getActivity(), OrderListActivity.class);
                intent.putExtra("model", 2);
                startActivity(intent);
                break;
            case R.id.tv_orderList:
                intent = new Intent(getActivity(), OrderListActivity.class);
                intent.putExtra("model", 0);
                startActivity(intent);
                break;
            case R.id.tv_changeInfo:
                intent = new Intent(getActivity(), EditInfoActivity.class);
                intent.putExtra("mode", 0);
                intent.putExtra("userID", SP.getUid(getActivity()));
                startActivity(intent);
                break;
            case R.id.ll_header_function:
                SP.setLoginUser(getActivity(), "", "", "");
                T.showShort(getActivity(), "登出成功");
                intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
                break;
        }
    }
}
